package com.useeinfo.oa.open.api.taskOrder;

import com.alibaba.fastjson.JSONArray;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.enums.ResultEnums;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import com.useeinfo.oa.modules.task.biz.TaskOrderBiz;
import com.useeinfo.oa.modules.task.biz.TaskWorkLogBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @auther: bin.liu
 * @Date: 2019/6/25 14:23
 * @Description: 任务工单app接口
 */
@Controller
@RequestMapping("/api/taskOrder")
public class TaskOrderAPI {




    @Autowired
    private TaskOrderBiz taskOrderBiz;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TaskWorkLogBiz taskWorkLogBiz;

    @Autowired
    private SystemUserBiz systemUserBiz;

    /**
     * 任务单详情
     */
    @RequestMapping("/taskOrderDetail")
    @ResponseBody
    public ResultDto taskOrderDetail(String taskOrderId) {
        return taskOrderBiz.taskOrderDetail(taskOrderId);
    }







    /**
     * 任务单列表接口
     * 1:待接单
     * 2:未完成
     * 3:审批中
     * 4:已结束
     */
    @RequestMapping("/taskOrderList")
    @ResponseBody
    public ResultDto taskOrderList(String pageIndex, String pageSize, String status, String searchParam,String date) {
        SystemUser systemUser = SessionKeyUtils.getSessionUser(request);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if(null == systemUser){
            return ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE);
        }
        if(null == enterprise){
            return ResultUtil.error("企业信息有误");
        }
        return taskOrderBiz.getTaskOrders(pageIndex, pageSize, status, searchParam,systemUser,enterprise,date);
    }

    /**
     * 创建任务单接口
     */
    @RequestMapping("/addTask")
    @ResponseBody
    public ResultDto addTaskOrder(String assignUserIds, String assignPositionIds, String orderName, String taskContent,
                                  String taskAddress, String photoIds, String recordingId, String areaId, String orderTypeId,String requireFinishTime) {
        SystemUser systemUser= SessionKeyUtils.getSessionUser(request);
        Enterprise enterprise=SessionKeyUtils.getSessionEnterprise(request);
        return taskOrderBiz.addTaskOrder(assignUserIds,assignPositionIds,orderName, taskContent, taskAddress, null,  photoIds, recordingId, areaId,orderTypeId,systemUser,enterprise,requireFinishTime);
    }


    /**
     * 可选参与人员列表——按权限查
     *
     * @param pageIndex
     * @param pageSize
     * @param userName
     * @return
     */
    @RequestMapping("/userJson")
    @ResponseBody
    public ResultDto userJson(String pageIndex, String pageSize, String userName) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (null == enterprise) {
            return ResultUtil.error("用户企业信息有误，请确认");
        }
        QueryParam queryParam = new QueryParam(pageIndex, pageSize);
        String hql = "select obj from SystemUser obj where obj.operationRole='common' and exists (select ep from obj.enterpriseSet ep where ep.id=" + enterprise.getId() + " ) " +
                "  and exists ( select ure from UserRoleEnterprise ure where ure.systemUser.id = obj.id and ure.enterprise.id = " + enterprise.getId() + "" +
                "  and exists( select auth from ure.systemRole.authoritySet auth where auth.authorityName='ROLE_ENTERPRISE_TASK_ORDER_TAKING')) ";

        if (!StringConverters.null2String(userName).equals("")) {
            hql += " and obj.userName like '%" + StringConverters.null2String(userName) + "%'";
        }

        List<SystemUser> systemUserList = systemUserBiz.hqlExcute(hql, queryParam);
        JSONArray jsonArray = new JSONArray();
        for (SystemUser systemUser : systemUserList) {
            jsonArray.add(systemUser.toJSONObject());
        }

        return ResultUtil.success(jsonArray);
    }

    /**
     * 参与人列表
     */
    @RequestMapping("/partnerList")
    @ResponseBody
    public ResultDto partnerList(String taskOrderId) {
        return taskOrderBiz.partnerList(taskOrderId);
    }

    /**
     * 添加参与人
     * @param taskOrderId
     * @param partnerArray 参与人json数据信息
     * @return
     */
    @RequestMapping("/addPartner")
    @ResponseBody
    public ResultDto addPartner(String taskOrderId, String partnerArray) {
        return taskOrderBiz.addPartner(taskOrderId, partnerArray);
    }

    /**
     * 移除参与人
     */
    @RequestMapping("/removePartner")
    @ResponseBody
    public ResultDto removePartner(String id) {
        return taskOrderBiz.removePartner(id);
    }

    /**
     * 字典下拉选接口
     */
    @RequestMapping("/getDictSelect")
    @ResponseBody
    public ResultDto getDictSelect(String typeCode) {
        return taskOrderBiz.getDictSelect(typeCode);
    }

    /**
     * 接单接口
     */
    @RequestMapping("/taking")
    @ResponseBody
    public ResultDto taking(String taskOrderId) {
        return taskOrderBiz.taking(taskOrderId);
    }

    /**
     * 到场
     */
    @RequestMapping("/presence")
    @ResponseBody
    public ResultDto presence(String taskOrderId, String longitude, String latitude) {
        return taskOrderBiz.presence(taskOrderId, longitude, latitude);
    }

    /**
     * 完成任务单》》到待审核状态
     */
    @RequestMapping("/completeTask")
    @ResponseBody
    public ResultDto completeTask(String taskOrderId, String participantInputs, String finishPhotos, String unFinishReason, String finishDescription) {
        return taskOrderBiz.completeTask(taskOrderId, participantInputs, finishPhotos, unFinishReason, finishDescription);
    }


    /**
     * 上传接口
     */
    @RequestMapping("/upload")
    @ResponseBody
    public ResultDto upload(MultipartFile file, String type) {
        return taskOrderBiz.upload(file, type);
    }

    /**
     * 区域列表
     */
    @RequestMapping("/areaList")
    @ResponseBody
    public ResultDto areaList(String pageIndex,String pageSize,String searchParam){
        return taskOrderBiz.areaList(pageIndex,pageSize,searchParam);
    }

    /**
     * 指派人员列表
     */
    @RequestMapping("/getUser")
    @ResponseBody
    public ResultDto getUser(String pageIndex, String pageSize, String userName, String userIds) {
        return taskOrderBiz.getUser(pageIndex,pageSize,userName,userIds);
    }

    /**
     * 指派岗位列表
     */
    @RequestMapping("/getPosition")
    @ResponseBody
    public ResultDto getPosition(String pageIndex, String pageSize,String positionIds) {
        return  taskOrderBiz.getPosition(pageIndex,pageSize,positionIds);
    }

    /**
     * 保存指定人
     */
    @RequestMapping("/saveAssignList")
    @ResponseBody
    public ResultDto saveAssignList(String assignIds, String taskOrderId) {
        return taskOrderBiz.saveAssignList(assignIds, taskOrderId);
    }

    /**
     * 保存指定岗位
     */
    @RequestMapping("/saveAssignPositionList")
    @ResponseBody
    public ResultDto saveAssignPositionList(String assignPositionIds, String taskOrderId) {
        return taskOrderBiz.saveAssignPositionList(assignPositionIds, taskOrderId);
    }

    /**
     * 参与人详情
     */
    @RequestMapping("/partnerDetail")
    @ResponseBody
    public ResultDto partnerDetail(String taskOrderId,String partnerId){
        return taskOrderBiz.partnerDetail(taskOrderId,partnerId);
    }

    @RequestMapping("/process")
    @ResponseBody
    public ResultDto process(String taskOrderId) {
        return taskOrderBiz.process(taskOrderId);
    }

    /**
     * 工作日志详情
     */
    @RequestMapping("/taskWorkLogDetail")
    @ResponseBody
    public ResultDto taskWorkLogDetail(String id) {
        return taskWorkLogBiz.taskWorkLogDetail(id);
    }

    /**
     * 工作日志列表
     */
    @RequestMapping("/taskWorkLogList")
    @ResponseBody
    public ResultDto taskWorkLogList(String pageIndex, String pageSize, String taskOrderId) {
        return taskWorkLogBiz.taskWorkLogList(pageIndex, pageSize, taskOrderId);
    }

    /**
     * 添加/修改工作日志
     */
    @RequestMapping("/addLog")
    @ResponseBody
    public ResultDto addLog(String logId, String workLogCatId, String workLogLevelId, String workContent, String photoIds, String taskOrderId) {
        return taskWorkLogBiz.addLog(logId, workLogCatId, workLogLevelId, workContent, photoIds, taskOrderId);
    }

    /**
     * 关注/取消关注
     */
    @RequestMapping("/focus")
    @ResponseBody
    public ResultDto focus(String taskOrderId, Boolean type) {
        return taskOrderBiz.focus(taskOrderId, type);
    }

    /**
     * 审核
     */
    @RequestMapping("/audit")
    @ResponseBody
    public ResultDto audit(String taskOrderId, Boolean type, String remark) {
        return taskOrderBiz.audit(taskOrderId, type, remark);
    }

    @RequestMapping("/finalAudit")
    @ResponseBody
    public ResultDto finalAudit(String taskOrderId, String score, String remark) {
        return taskOrderBiz.finalAudit(taskOrderId, remark, score);
    }
}
